﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Classified.Data;
using Classified.Data.Repositories;
using Classified.Domain.Entities;
using Classified.Web;
using MvcPaging;

namespace Classified.Web.Areas.Admin.Controllers
{
    [CustomAuthorize(RequiredRole = "Admin")]
    public class PageController : Controller
    {
        private readonly IPageRepositories _pageRepositories;
        public PageController(IPageRepositories pageRepositories)
        {
            _pageRepositories = pageRepositories;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }
        public ActionResult List(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var data = _pageRepositories.GetAll().OrderByDescending(i => i.Id).ToPagedList(currentPageIndex, 10);

            return View(data);
        }

      

        public ActionResult Create()
        {
            var data = new Page();
            return View(data);
        }

        //
        // POST: /Admin/Page/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Page model)
        {
            if (ModelState.IsValid)
            {
                if (!IsValidPageName(model.Name))
                {
                    ModelState.AddModelError("Name", "Page name already exist please use different name");
                    return View(model);
                }
                model.CreatedOnUtc = DateTime.UtcNow;
                _pageRepositories.Add(model);
                return RedirectToAction("List");
            }
            return View(model);
        }
        public bool IsValidPageName(string name)
        {
            if (_pageRepositories.Filter(x => x.Name == name).Any())
                return false;
            return true;

        }
        public bool IsValidPageNameUpdate(int id, string currentname)
        {
          
            var olddata = _pageRepositories.GetById(x => x.Id == id);
            if (olddata.Name != currentname)
            {
                if (_pageRepositories.GetMany(x => x.Name == currentname).Any())
                    return false;

            }
            return true;
           
        }

        
        public ActionResult Edit(int id)
        {
            var data = _pageRepositories.GetById(id);
            if (data != null)
                return View(data);
            return RedirectToAction("List");
        }

    
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Page model)
        {
            if (ModelState.IsValid)
            {
                if (!IsValidPageNameUpdate(model.Id, model.Name))
                {
                    ModelState.AddModelError("Name", "Page name already exist please use different name");
                    return View(model);
                }

                var page = new Page()
                               {
                                   Id = model.Id,
                                   Body = model.Body,
                                   CreatedOnUtc = model.CreatedOnUtc,
                                   IsActive = model.IsActive,
                                   IsDefault = model.IsDefault,
                                   MetaDescription = model.MetaDescription,
                                   MetaKeywords = model.MetaKeywords,
                                   MetaTitle = model.MetaTitle,
                                   Name = model.Name,
                                   Title = model.Title,
                                   PageDesc = model.PageDesc,
                                   UpdatedOnUtc = DateTime.UtcNow

                               };
                var context = new ClassifiedContext();
                context.Entry(page).State = EntityState.Modified;
                context.SaveChanges();
                
                return RedirectToAction("List");
            }
            return View(model);
        }

        //
        // GET: /Admin/Page/Delete/5

        public ActionResult Delete(int id)
        {
            var data = _pageRepositories.GetById(id);
            if (data != null)
                _pageRepositories.Delete(data);
            return RedirectToAction("List");
        }


    }
}
